package dgut.party.business.entity.base;

import com.fasterxml.jackson.annotation.JsonFormat;
import dgut.party.business.common.enumeration.Gender;
import dgut.party.business.entity.rbac.Role;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

@Getter
@Setter
@Entity
@ToString
@SuperBuilder
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Account extends BaseEntity {

  private static final long serialVersionUID = 1010239914012835382L;

  @Column(name = "dtype", insertable = false, updatable = false)
  protected String type;

  protected boolean enabled;
  @Column(nullable = false, length = 20)
  protected String username;

  @Column(nullable = false, length = 256)
  protected String password;

  @ManyToMany(fetch = FetchType.LAZY)
  @JoinTable(name = "account_roles", joinColumns = @JoinColumn(name = "account_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
  protected Set<Role> roles;

  // Info ----------------
  @Column(length = 20)
  protected String realName;

  @Enumerated(EnumType.STRING)
  protected Gender gender;
  @Column(length = 15, unique = true)
  protected String phone;
  @Column(unique = true)
  protected String weixin;
  @Column(unique = true)
  protected String qq;

  protected String avatar;

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  protected Date lastLoginTime;

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  protected Date lastOperateTime;

  protected String lastOperateUrl;

  @PrePersist
  @Override
  protected void beforePersist() {
    super.beforePersist();
    this.enabled = true;
    this.avatar = "";
  }
}
